package org.jeecg.modules.wlhy.report;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.jg.xz.park.XzIndustrialParkService;
import org.jeecg.modules.third.jg.xz.park.dto.AppealDTO;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
import org.jeecg.modules.wlhy.company.service.IHyCompanyService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.entity.UpDataShippingNoteDTO;
import org.jeecg.modules.wlhy.shipping.entity.UpDataShippingNoteDTO2;
import org.jeecg.modules.wlhy.shipping.mapper.HyShippingNoteMapper;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class JgService{

    private static final Logger LOGGER = LoggerFactory.getLogger(JgService.class);

    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Resource
    private HyShippingNoteMapper hyShippingNoteMapper;

    /**
     * 申报企业信息
     */
    @Autowired
    private IHyCompanyService companyService;

    @Autowired
    private XzIndustrialParkService xzIndustrialParkService;


    @Autowired
    private IHyCompanyConfigService companyConfigService;

    /**
     * 按公司上报监管平台
     *
     * @param hyShippingNote
     * @return
     */
    @Deprecated
    public boolean uploadByCompany(HyShippingNote  hyShippingNote) {
        HyShippingNote hyShippingNoteDb = hyShippingNoteService.getById(hyShippingNote.getId());
        IReportService reportService = getReportService(hyShippingNoteDb);
        return reportService.report(hyShippingNoteDb);
    }

    public Result uploadShippingNote(HyShippingNote hyShippingNote) {
        IReportService reportService = getReportService(hyShippingNote);
        log.info("开始上报运单数据，{}", reportService.getReportType().getMsg());
        return reportService.reportShippingNote(hyShippingNote);
    }

    public Result uploadCapitalFlow(HyShippingNote hyShippingNote) {
        IReportService reportService = getReportService(hyShippingNote);
        log.info("开始上报资金流水数据，{}", reportService.getReportType().getMsg());
        return reportService.reportCapitalFlow(hyShippingNote);
    }


    public Result uploadShippingNoteHuaibei(HyShippingNote hyShippingNote) {
        IReportService reportService = ReportServiceFactory.getReport(ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
        log.info("开始上报淮北运单数据，{}", reportService.getReportType().getMsg());
        return reportService.reportShippingNote(hyShippingNote);
    }

    public Result uploadCapitalFlowHuaibei(HyShippingNote hyShippingNote) {
        IReportService reportService = ReportServiceFactory.getReport(ReportPlatformEnum.REPORT_PLATFORM_HUAIBEI.getCode());
        log.info("开始上报淮北资金流水数据，{}", reportService.getReportType().getMsg());
        return reportService.reportCapitalFlow(hyShippingNote);
    }

    public Result uploadParkShippingNote(HyShippingNote hyShippingNote) {
        //获取园区上报服务
        IReportService reportService = getParkReportService(hyShippingNote);
        log.info("开始上报园区运单数据，{}", reportService.getReportType().getMsg());
        return reportService.reportShippingNote(hyShippingNote);
    }

    public Result uploadParkCapitalFlow(HyShippingNote hyShippingNote, String uploadBankHdImg) {
        //获取园区上报服务
        IReportService reportService = getParkReportService(hyShippingNote);
        log.info("开始上报园区资金流水数据，{}", reportService.getReportType().getMsg());
        return reportService.reportCapitalFlow(hyShippingNote, uploadBankHdImg);
    }

    public Result checkParkShippingNote(HyShippingNote hyShippingNote) {
        //校验中交园区运单数据
        IReportService reportService = getParkReportService(hyShippingNote);
        log.info("开始校验园区运单数据，{}", reportService.getReportType().getMsg());
        HyShippingNote hyShippingNoteDb = hyShippingNoteService.getById(hyShippingNote.getId());
        return reportService.validateShippingNote(hyShippingNoteDb);
    }

    public Result appealShippingNote(HyShippingNote hyShippingNote, String appealContext, String appealImg) {
        //中交园区运单申诉
        IReportService reportService = getParkReportService(hyShippingNote);
        log.info("开始园区运单申诉，{}", reportService.getReportType().getMsg());

        return reportService.appealShippingNote(
				new AppealDTO().setTransportNumber(hyShippingNote.getShippingNoteNumber())
				.setSerialNumber("0000")
				.setAppealContext(appealContext)
				.setAppealImg(appealImg));
    }

    public Result appealShippingNoteQuery(HyShippingNote hyShippingNote) {
        //中交园区运单申诉查询
        IReportService reportService = getParkReportService(hyShippingNote);
        log.info("开始园区运单申诉查询，{}", reportService.getReportType().getMsg());

        return reportService.appealShippingNoteQuery(hyShippingNote.getId());
    }


    private IReportService getReportService(HyShippingNote hyShippingNoteDb) {
        if (hyShippingNoteDb == null) {
            throw new JeecgBootException("该运单上报信息不能为空");
        }
        //申报公司
        HyCompanyConfig companyConfig = companyConfigService.getOne(new QueryWrapper<HyCompanyConfig>()
                .eq("company_id",hyShippingNoteDb.getCompanyId())
        );

        if (companyConfig == null) {
            throw new JeecgBootException(hyShippingNoteDb.getShippingNoteNumber() + "上报公司配置信息不存在");
        }

        log.warn("按公司上报配置，{}", JSONUtil.toJsonStr(companyConfig));
        //hyShippingNoteDb.setCompanyId(hyShippingNoteDb.getCompanyId());
        //在getReport中做判断 如果report==null，抛异常
        IReportService reportService = ReportServiceFactory.getReport(companyConfig.getReportPlatformType());
        reportService.checkReport();
        reportService.setReportInfo(companyConfig);
        return reportService;
    }


    private IReportService getParkReportService(HyShippingNote hyShippingNoteDb) {
        if (hyShippingNoteDb == null) {
            throw new JeecgBootException("该运单上报信息不能为空");
        }

        //上报公司
        HyCompanyConfig companyConfig = companyConfigService.getOne(new QueryWrapper<HyCompanyConfig>()
                .eq("company_id", hyShippingNoteDb.getCompanyId())
        );

        if (companyConfig == null) {
            throw new JeecgBootException(hyShippingNoteDb.getShippingNoteNumber() + "上报公司配置信息不存在");
        }

        log.warn("园区上报配置，{}", JSONUtil.toJsonStr(companyConfig));

        IReportService reportService = ReportServiceFactory.getReport(companyConfig.getParkReportPlatformType());
        reportService.checkReport();
        return reportService;
    }


    public List<UpDataShippingNoteDTO> getList(QueryWrapper<UpDataShippingNoteDTO> queryWrapper) {
        return hyShippingNoteMapper.getUpDataPageList(queryWrapper);
    }
    public List<UpDataShippingNoteDTO2> getList1(QueryWrapper<UpDataShippingNoteDTO2> queryWrapper) {
        return hyShippingNoteMapper.getUpDataPageList1(queryWrapper);
    }


    /**
     * 监管平台数据上报状态信息
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<UpDataShippingNoteDTO> getUpDataPageList(Page<UpDataShippingNoteDTO> page,
                                                          QueryWrapper<UpDataShippingNoteDTO> queryWrapper) {
        IPage<UpDataShippingNoteDTO> pageList = hyShippingNoteMapper.getUpDataPageList(page, queryWrapper);
        return pageList;
    }


    /**
     * 园区数据上报状态信息
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<UpDataShippingNoteDTO> getParkUpDataPageList(Page<UpDataShippingNoteDTO> page,
                                                          QueryWrapper<UpDataShippingNoteDTO> queryWrapper) {
        IPage<UpDataShippingNoteDTO> pageList = hyShippingNoteMapper.getParkUpDataPageList(page, queryWrapper);
        return pageList;

    }
    /**
     * 淮北数据上报状态信息
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<UpDataShippingNoteDTO> getHuaibeiUpDataPageList(Page<UpDataShippingNoteDTO> page,
                                                          QueryWrapper<UpDataShippingNoteDTO> queryWrapper) {
        IPage<UpDataShippingNoteDTO> pageList = hyShippingNoteMapper.getHuaibeiUpDataPageList(page, queryWrapper);
        return pageList;
    }

    /**
     * 鄂尔多斯市监管上报状态信息
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<UpDataShippingNoteDTO> getOrdosUpDataPageList(Page<UpDataShippingNoteDTO> page,
                                                                 QueryWrapper<UpDataShippingNoteDTO> queryWrapper) {
        IPage<UpDataShippingNoteDTO> pageList = hyShippingNoteMapper.getOrdosUpDataPageList(page, queryWrapper);
        return pageList;
    }


}
